#pragma once
#include <QObject>
#include <map>

#include "../widgets.h"

enum ColWidthType { kFixed, kAuto, kManual };

enum CtxMenu { kNone, kDft, KALl };
struct S_WIDGETS_EXPORT ColumnInfo {
  QString name;
  QString tooltip;
  int id = -1;
  int index = 0;  // 递增，不能跳
  int curIndex = 0;
  int group = 0;
  float ratio = -1;
  int minWidth = -1;
  int dftWidth = -1;
  int curWidth = -1;
  bool hide = false;
  Qt::Alignment align = Qt::AlignVCenter | Qt::AlignLeft;
  ColWidthType widthType = kManual;

  ColumnInfo() = default;
  ColumnInfo(const ColumnInfo&) = default;
};

#define BEGIN_COLUMN int colIndex = 0;

#define ADD_COLUMN(id1, colSet, name1, group1, dftWidth1, minWidth1, ratio1, \
                   hide1, align1, widthType1, tooltip1)                      \
  {                                                                          \
    ColumnInfo ci;                                                           \
    ci.id = id1;                                                             \
    ci.index = colIndex;                                                     \
    ci.name = name1;                                                         \
    ci.group = group1;                                                       \
    ci.ratio = (float)ratio1;                                                \
    ci.minWidth = minWidth1;                                                 \
    ci.dftWidth = dftWidth1;                                                 \
    ci.hide = hide1;                                                         \
    ci.align = align1;                                                       \
    ci.widthType = widthType1;                                               \
    ci.curWidth = dftWidth1;                                                 \
    ci.curIndex = colIndex;                                                  \
    ci.tooltip = tooltip1;                                                   \
    colSet->addColumnInfo(ci);                                               \
    ++colIndex;                                                              \
  }

class SVirtualTable;
class S_WIDGETS_EXPORT STableColumnSet : public QObject {
  Q_OBJECT
 public:
  STableColumnSet(SVirtualTable*, const QString&, QObject*);
  void addColumnInfo(const ColumnInfo&);
  void setColumnHide(int id, bool hide);
  void setTableWidthFixed(bool fixed);
  void apply(bool loadCfg = true);
  void setSaveCfg(bool v);
  void restoreDft();
  void resizeColumn(bool useDft = false);
  void applyChange(std::map<int, bool>);
  bool isColVisible(int index);
  bool isColVisible2(int id);
  ColumnInfo colInfoByIndex(int index);
  ColumnInfo colInfoById(int id);
  void setMenuCfg(CtxMenu cfg) { m_menuCfg = cfg; }
  void setFrozenCols(int cols);  // 最后调用

  std::map<int, std::vector<ColumnInfo> > groups();

 protected:
  bool eventFilter(QObject* watched, QEvent* event) override;
  void attachFrozenCols();

  void saveCfg();
  void loadCfg();
 Q_SIGNALS:
  void clearHolidayMap();
  void tableSet();

 protected slots:
  void onHeaderSectionMoved(int logicalIndex, int oldVisualIndex,
                            int newVisualIndex);
  void onHeaderSectionResized(int logicalIndex, int oldSize, int newSize);

 private:
  QString m_idStr;
  std::map<int, ColumnInfo> m_dftColumnInfos;  // first is logic index
  std::map<int, ColumnInfo> m_curColumnInfos;  // first is visual index
  SVirtualTable* m_table = nullptr;
  bool m_tableWidthFixed = false;  // 根据Table的宽带自动调整列宽
  bool m_saveCfg = false;
  bool m_doResize = false;
  bool m_doApply = false;
  bool m_dragHeader = false;
  CtxMenu m_menuCfg = KALl;
};